﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CollectionGuard.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Guard class used to validate arguments supplied to methods common to collection classes.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Collections
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Guard class used to validate arguments supplied to methods common to collection classes.
    /// </summary>
    public static class CollectionGuard
    {
        /// <summary>
        /// Checks the arguments supplied to <see cref="ICollection{T}.CopyTo"/>.
        /// </summary>
        /// <param name="array">The array that was passed to the <b>CopyTo</b> method.</param>
        /// <param name="arrayIndex">The arrayIndex that was passed to the <b>CopyTo</b> method.</param>
        /// <param name="count">The number of item currently in the collection.</param>
        public static void CopyToInvalidArguments(Array array, int arrayIndex, int count)
        {
            CopyToInvalidArguments(array, arrayIndex, count, "array", "arrayIndex");
        }

        /// <summary>
        /// Checks the arguments supplied to <see cref="ICollection{T}.CopyTo"/>.
        /// </summary>
        /// <remarks>
        /// When implementing <see cref="ICollection{T}.CopyTo"/> the arguments <b>array</b> and <b>arrayIndex</b>
        /// must be checked against source collection. This method performs the checks required and raises 
        /// appropriate exceptions. 
        /// </remarks>
        /// <param name="array">The array that was passed to the <b>CopyTo</b> method.</param>
        /// <param name="arrayIndex">The arrayIndex that was passed to the <b>CopyTo</b> method.</param>
        /// <param name="count">The number of items currently in the collection.</param>
        /// <param name="arrayArgumentName">The name of the array argument passed to the <b>CopyTo</b> method.</param>
        /// <param name="arrayIndexArgumentName">The name of the arrayIndex argument passed to the <b>CopyTo</b> method.</param>
        public static void CopyToInvalidArguments(Array array, int arrayIndex, int count, string arrayArgumentName, string arrayIndexArgumentName)
        {
            // provide defaults to argument names if not supplied
            if (arrayArgumentName == null)
            {
                arrayArgumentName = "array";
            }

            // check for null
            Guard.ArgumentNull(arrayArgumentName, array);

            // check index
            if ((arrayIndex < 0) || (arrayIndex > array.Length))
            {
                throw CollectionExceptions.IndexOutOfRange(arrayIndex);
            }

            // check space
            if ((array.Length - arrayIndex) < count)
            {
                throw CollectionExceptions.CollectionCopyToArrayInsufficientSpace(arrayArgumentName);
            }

            // check rank (number of dimensions)
            if (array.Rank != 1)
            {
                throw CollectionExceptions.CollectionCopyToArrayRankInvalid(arrayArgumentName);
            }
        }
    }
}
